//////////////////////////////////////////////////////////////////////////////////
// Who  : Lim, Dong-moon (dmlim@nate.com, 011-701-9381)
// When : 2005/11/29
//////////////////////////////////////////////////////////////////////////////////

#ifndef __INFO_HEADER_H__
#define __INFO_HEADER_H__

#ifdef WIN32

#include <windows.h>
#include <wincrypt.h>

#else

#include <ncrypt_types.h>

#endif

#include <nblob.h>

#define		LATEST_NCRYPT_VER	3

//////////////////////////////////////////////////////////////////////////////////////

typedef struct _NCRYPT_ALGORITHMS
{
	USHORT	len;
	ALG_ID*	pAlgs;
} NCRYPT_ALGORITHMS;

typedef struct _NCRYPT_CERTIFICATES
{
	USHORT len;
	NBLOB* pBlobs;
} NCRYPT_CERTIFICATES;

typedef struct _NCRYPT_KEYHEADER
{
	NCRYPT_ALGORITHMS algs;
	NBLOB	hash;				// added for version 2 nego
	NCRYPT_CERTIFICATES certs;
	NBLOB	reserved;
} NCRYPT_KEYHEADER;

typedef struct _NCRYPT_KEYINFO
{
	DWORD				version;
	NCRYPT_KEYHEADER	header;
	NBLOB				body;
} NCRYPT_KEYINFO;

typedef struct _NCRYPT_MSGHEADER
{
	NBLOB	reserved;
} NCRYPT_MSGHEADER;

typedef struct _NCRYPT_MSGINFO
{
	DWORD				version;
	NCRYPT_MSGHEADER	header;
	NBLOB				body;
} NCRYPT_MSGINFO;

typedef struct _NDRM_CONTET_HEADER
{
	DWORD version;
	DWORD algId;
	NBLOB key;
} NDRM_CONTENT_HEADER;

void FreeKeyHeader(NCRYPT_KEYHEADER *pHeader);
void FreeKeyInfo(NCRYPT_KEYINFO *pKeyInfo);
void FreeMsgHeader(NCRYPT_MSGHEADER *pHeader);
void FreeMsgInfo(NCRYPT_MSGINFO* pMsgInfo);
int EncodeMsgHeader(NCRYPT_MSGHEADER *pHeader, BYTE* pAddr);
int EncodeMsgInfo(NCRYPT_MSGINFO *pMsgInfo, BYTE* pAddr);
BOOL DecodeMsgHeader(NCRYPT_MSGHEADER *pHeader, BYTE* pAddr, DWORD cSize);
BOOL DecodeMsgInfo(NCRYPT_MSGINFO* pMsgInfo, BYTE* pAddr, DWORD cSize);
int EncodeKeyHeader(NCRYPT_KEYHEADER *pHeader, BYTE* pAddr, DWORD dwVersion = LATEST_NCRYPT_VER);
int EncodeKeyInfo(NCRYPT_KEYINFO* pKeyInfo, BYTE* pAddr);
BOOL DecodeKeyHeader(NCRYPT_KEYHEADER *pHeader, BYTE* pAddr, DWORD cSize, DWORD version, BOOL bForce = FALSE);
BOOL DecodeKeyInfo(NCRYPT_KEYINFO* pKeyInfo, BYTE* pAddr, DWORD cSize, BOOL bForce = FALSE);
void DestroyDRMHeader(NDRM_CONTENT_HEADER* pHeader);
DWORD GetDRMHeaderSize(NDRM_CONTENT_HEADER* pHeader);
NBLOB* EncodeDRMHeader(NDRM_CONTENT_HEADER* pHeader);
NDRM_CONTENT_HEADER* DecodeDRMHeader(BYTE* pbData, DWORD cbSize, DWORD* cbRead);
NCRYPT_ALGORITHMS* DecodeAlgorithms(BYTE* pData, DWORD cSize, DWORD blobType);
void DestroyAlgorithm(NCRYPT_ALGORITHMS* pNcryptAlgs);

#endif //__INFO_HEADER_H__
